package zlx.springcloud.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import zlx.springcloud.dao.OderDao;
import zlx.springcloud.domain.Oder;
import zlx.springcloud.service.AccountService;
import zlx.springcloud.service.OderService;
import zlx.springcloud.service.StorageService;

import javax.annotation.Resource;

@Service
@Slf4j
public class OderServiceImpl implements OderService {
    @Resource
    private OderDao oderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;


    @Override
    @GlobalTransactional(name = "seata-account-service",rollbackFor = Exception.class)
    public void create(Oder oder) {
        //1、新建订单
        log.info("*****>>>开始新建订单");
        oderDao.create(oder);

        //扣除库存
        log.info("******>>>订单微服务调用库存开始，减少库存");
        storageService.decrease(oder.getProductId(),oder.getCount());
        log.info("******>>>订单微服务开始，减少库存end");

        //修改订单状态，从0到1，1代表已完成订单支付
        log.info("******>>>");
        oderDao.update(oder.getUserId(),0);
        log.info("******>>>修改订单结束");

        //扣钱
        log.info("******>>>订单微服务调用客户账号余额开始，收款扣钱");
        accountService.decrease(oder.getUserId(),oder.getMoney());
        log.info("******>>>订单微服务调用客户账号余额开始，收款扣钱end");


        log.info("******>>>创建订单结束，欢迎购买");
    }
}
